package main;

import Bean.UserInfoBean;
import utils.DBUtils;

import java.sql.SQLException;
import java.util.Scanner;

public class AdminMange{

    static Scanner sc = new Scanner(System.in);
    static DBUtils dbUtils;
    public static void manageUser(UserInfoBean admin){
        adminBreak: while(true) {
            System.out.println("请选择你要做什么：");
            System.out.println("1 - 修改用户权限");
            System.out.println("2 - 修改用户密码");
            System.out.println("3 - 修改用户支付密码");
            System.out.println("4 - 删除用户");
            System.out.println("5 - 回到上一步");
            System.out.println("\n\n\n\n");
            switch (sc.nextInt()) {
                case 1:
                    changeUserPower(admin);
                    break adminBreak;
                case 2:
                    changeUserPassword(admin);
                    break adminBreak;
                case 3:
                    changeUserPwd(admin);
                    break adminBreak;
                case 4:
                    delUser(admin);
                    break adminBreak;
                case 5:
                    Admin.mainScreen(admin);
                    break adminBreak;
                default:
                    System.out.println("不要胡乱输入哦！！！");
                    break;
            }
        }
    }


    public static void changeUserPower(UserInfoBean admin){
        System.out.println("请输入要更改的用户的id：");
        String uid = sc.next();
        String sql = "select * from userinfo where `Del` = 0 and ID = " +uid+" ";
        try {
            UserInfoBean user = dbUtils.selectUser(sql);
            if(user == null){
                System.out.println("数据库中没有此人的id，请检查是否输错 或 该用户已被删除");
            }else{
                System.out.println("要将其改为什么");
                System.out.println("1 - 普通用户");
                System.out.println("2 - 管理员");

                int power = sc.nextInt();
                dbUtils.updateUser("Power" , String.valueOf(power - 1) , user.getID());
                System.out.println("修改完成");
            }
        }catch (SQLException e){
            System.out.println("数据库没有此用户");
        }
        manageUser(admin);
    }

    public static void changeUserPassword(UserInfoBean admin){
        System.out.println("请输入要更改的用户的id：");
        String uid = sc.next();
        String sql = "select * from userinfo where `Del` = 0 and ID = " +uid+" ";
        try {
            UserInfoBean user = dbUtils.selectUser(sql);
            if(user == null){
                System.out.println("数据库中没有此人的id，请检查是否输错 或 该用户已被删除");
            }else{
                // 输入密码
                System.out.println("请输入新密码：");
                String pw1 = sc.next();
                // 确认密码
                System.out.println("请确认新密码：");
                String pw2 = sc.next();

                while(!pw1.equals(pw2)){
                    System.out.println("两次密码不同，请重新输入！！！！");
                    // 输入密码
                    System.out.println("请输入新密码：");
                    pw1 = sc.next();
                    // 确认密码
                    System.out.println("请确认新密码：");
                    pw2 = sc.next();
                }

                dbUtils.updateUser("Password" , pw1 , user.getID());
                System.out.println("修改完成");
            }
        }catch (SQLException e){
            System.out.println("数据库没有此用户");
        }
        manageUser(admin);
    }

    public static void changeUserPwd(UserInfoBean admin){
        System.out.println("请输入要更改的用户的id：");
        String uid = sc.next();
        String sql = "select * from userinfo where `Del` = 0 and ID = " +uid+" ";
        try {
            UserInfoBean user = dbUtils.selectUser(sql);
            if(user == null){
                System.out.println("数据库中没有此人的id，请检查是否输错 或 该用户已被删除");
            }else{
                // 输入密码
                System.out.println("请输入新支付密码：");
                String pw1 = sc.next();
                // 确认密码
                System.out.println("请确认新支付密码：");
                String pw2 = sc.next();

                while(!pw1.equals(pw2)){
                    System.out.println("两次密码不同，请重新输入！！！！");
                    // 输入密码
                    System.out.println("请输入新支付密码：");
                    pw1 = sc.next();
                    // 确认密码
                    System.out.println("请确认新支付密码：");
                    pw2 = sc.next();
                }

                dbUtils.updateUser("Pwd" , pw1 , user.getID());
                System.out.println("修改完成");
            }
        }catch (SQLException e){
            System.out.println("数据库没有此用户");
        }
        manageUser(admin);
    }

    public static void delUser(UserInfoBean admin){
        System.out.println("请输入要删除的用户的id：");
        String uid = sc.next();
        String sql = "select * from userinfo where `Del` = 0 and ID = " +uid+" ";
        try {
            UserInfoBean user = dbUtils.selectUser(sql);
            if(user == null){
                System.out.println("数据库中没有此人的id，请检查是否输错 或 该用户已被删除");
            }else{
                delBreak :while(true) {
                    System.out.println("是否确认删除？(y/n)");
                    switch (sc.next().charAt(0)) {
                        case 'y':
                            dbUtils.updateUser("Del", "1", user.getID());
                            System.out.println("修改完成");
                            break delBreak;
                        case 'n':
                            System.out.println("ok！");
                            break delBreak;
                        default:
                            System.out.println("无效输入");
                    }
                }
            }
        } catch (SQLException e){
            System.out.println("数据库没有此用户");
        }
        manageUser(admin);
    }


}
